//主页
import React, { Component } from 'react';
import {
    View,
    Text,
    ListView,
    TouchableOpacity,
    RefreshControl,                         //刷新小组件
    ActivityIndicator
} from 'react-native';

import { homefourStyle as styles } from '../../../../styles';
import Icon from 'react-native-vector-icons/Ionicons';
import Homefour_1 from './homefour_1';

import { ShowoPostion } from '../../../actions/postionAction';      //加载action
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

var Dimensions = require('Dimensions');
var width = Dimensions.get('window').width;
var height = Dimensions.get('window').height;

const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => { r1 !== r2 } });//listView 数据判断方式
class Homefour extends Component {
    // 初始化模拟数据
    constructor(props) {
        super(props);
        this.state = {
            uploading: true, //下拉刷新按钮状态 (初始化加载时处在loading状态)
            dwloading: false, //上拉刷新按钮状态
        }
        this.isFirst = true                         //页面第一次加载
    }

    //加载后渲染页面
    componentDidMount() {
        const { actions } = this.props;
        actions.ShowoPostion({ page: 1 }); //初始化查询
    }

    render() {
        //获取用户列表数据及是否等待状态
        const { postionList, isloading } = this.props.postions.data;
        var a = new Array();
        for (var i = 0; i < 3; i++) {
            a.push(postionList[i]);
        }
        //创建listView数据源
        const dataSource = ds.cloneWithRows(a);
        return (
            <View style={styles.homefourall}>
                <View style={styles.fourtop}>
                    <Text style={styles.fourin}>每日新职</Text>
                    <TouchableOpacity
                        style={styles.fourin2}
                        activeOpacity={1}
                        onPress={() => { this.props.navigation.navigate('Postion') }}
                    >
                        <Text style={styles.fourint}>查看更多</Text>
                        <Icon
                            name={'ios-arrow-forward'}
                            size={17}
                            color={'#3d9ccc'} />
                    </TouchableOpacity>
                </View>

                <ListView
                    refreshControl={                            //是否更新
                        <RefreshControl                         //刷新控件
                            refreshing={this.state.uploading && this.props.postions.isloading}                  //刷新时候的小控件
                        />}
                    enableEmptySections={true}
                    dataSource={dataSource}         //数据源(数组)
                    renderRow={this.renderRow.bind(this)}                 // 遍历数组的回调函数
                    contentContainerStyle={styles.homefoure}
                />
            </View>
        );
    }

    //刷新隐藏按钮
    componentDidUpdate() {
        if (this.state.uploading && !this.props.postions.isloading) {
            this.setState({
                uploading: false //下拉刷新按钮状态：隐藏
            })
        }
        if (this.state.dwloading && !this.props.postions.isloading) {
            this.setState({
                dwloading: false //上拉刷新按钮状态：隐藏
            })
        }
    }

    //下拉刷新开始
    _onRefresh() {
        this.props.postions.isloading = true //设置为加载状态
        const { actions } = this.props;
        actions.ShowoPostion({ page: 1 });
        this.setState({
            uploading: true //下拉刷新按钮状态：显示
        });
    }

    renderFooter() {
        if (!this.props.postions.ret) {
            //console.log("请求失败");
        }
        if (this.state.dwloading) {
            return (<View style={{ marginVertical: 10 }}>
                <ActivityIndicator
                    animating={true}
                    style={{ height: 10, alignItems: 'center', justifyContent: 'center', padding: 1, }}
                    size="small" />
            </View>);
        }
        if (this.state.uploading) {
            return (<View style={{ marginVertical: 10, height: 100, alignItems: 'center', justifyContent: 'flex-end' }}>
                <Text>玩命加载ing</Text>
            </View>);
        }
        return (<View style={{ marginVertical: 10 }} />);
    }

    //list子项加载方法
    renderRow(rowData) {
        return (
            <Homefour_1
                postion={rowData}
                navigation={this.props.navigation}
            />
        )
    }
}

//组合action
var actions = {
    ShowoPostion
}

//连接state到props
const mapStateToProps = state => {
    return {
        postions: state.ShowPostion             //自己的reducers
    }
}

//连接action
const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators(actions, dispatch)
    }
}

//导出组合好的组件(组合后状态改变重新渲染组合组件的页面)
export default connect(
    mapStateToProps, mapDispatchToProps
)(Homefour);